<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-form :inline="true">
        <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="startAdd">添加</el-button>
        <el-button class="filter-item" style="float:right" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
        <el-form-item label="手机号" style="float:right">
          <el-input v-model="listQuery.mobile" clearable class="filter-item" style="width: 120px;" placeholder=""/>
        </el-form-item>
        <el-form-item label="用户名" style="float:right">
          <el-input v-model="listQuery.username" clearable class="filter-item" style="width: 120px;" placeholder=""/>
        </el-form-item>

      </el-form>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column align="center" label="用户名" prop="username" />

      <el-table-column align="center" label="真实姓名" prop="realName" />

      <el-table-column align="center" label="手机号码" prop="mobile"/>

      <el-table-column align="center" label="角色名称" prop="roleName"/>

      <el-table-column align="center" label="头像">
        <template slot-scope="scope">
          <span><img :src="scope.row.avatar" width="30" height="30" style="margin-left:50px;display: list-item;border-radius:50%;"></span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="状态" width="100" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status==1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="isStatusSwitch(scope.row.id, scope.row.status)"/>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="startEditAccount(scope.row)">编辑</el-button>
          <el-button type="primary" size="small" @click="deleteAccount(scope.row.id, scope.row.username)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!--<pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList" />-->

    <el-dialog :close-on-click-modal="false" :title="dialogTitle" :visible.sync="dialogFormVisible" width="40%">
      <el-form :inline="true" status-icon>
        <el-row type="flex" justify="center">
          <el-col :span="22">
            <el-form-item label="* 用户名 " label-width="10em">
              <el-input v-model="accountModel.username" clearable class="filter-item" style="max-width: 260px;" placeholder=""/>
            </el-form-item>
            <el-form-item v-if="dialogType==1" label="* 密码 " label-width="10em">
              <el-input v-model="accountModel.password" clearable class="filter-item" style="max-width: 260px;" placeholder=""/>
            </el-form-item>
            <el-form-item label="* 真实姓名 " label-width="10em">
              <el-input v-model="accountModel.realName" clearable class="filter-item" style="max-width: 260px;" placeholder=""/>
            </el-form-item>
            <el-form-item label="* 手机号 " label-width="10em">
              <el-input v-model="accountModel.mobile" clearable class="filter-item" style="max-width: 260px;" placeholder=""/>
            </el-form-item>

            <el-form-item label="* 角色名称 " label-width="10em">
              <el-select v-model="accountModel.roleId" placeholder="请选择" style="width: 120px;">
                <el-option
                  v-for="item in roleList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"/>
              </el-select>
            </el-form-item>
            <br>
            <el-form-item label="头像:" label-width="10em">
              <el-upload
                ref="upload"
                :multiple="false"
                :file-list="fileList"
                :http-request = "doFileUpload"
                :auto-upload="true"
                :show-file-list="false"
                action="">
                <el-button size="small" type="primary">上传照片</el-button>
              </el-upload>
            </el-form-item>
            <br>
            <el-form-item label=" " label-width="10em">
              <img :src="accountModel.avatar" style="width:10em; height:10em;">
            </el-form-item>

          </el-col>
        </el-row>

      </el-form>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="confirm(); dialogFormVisible = false">确定</el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { listAdmin, createAdmin, updateAdmin, deleteAdmin, updateStatusAdmin, getRoleList } from '@/api/admin'
import { fileUpload } from '@/api/knowledge'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Account',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20
      },
      downloadLoading: false,
      dialogFormVisible: false,
      roleList: [],
      dialogType: 1,
      dialogTitle: '',
      editSupplierId: 1,
      milkName: '',
      babySuppliesType: 1,
      accountModel: {}
    }
  },
  created() {
    this.getList()
    this.getRoleList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listAdmin(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.data.length
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    getRoleList() {
      this.listLoading = true
      const emptyData = {}
      getRoleList(emptyData).then(response => {
        this.roleList = response.data.data
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    startEditAccount(accountModel) {
      this.dialogFormVisible = true
      this.dialogType = 2
      this.dialogTitle = '编辑'
      this.accountModel = accountModel
    },
    doFileUpload(file) {
      fileUpload(file).then(response => {
        this.accountModel.avatar = response.data.data
        this.$forceUpdate()
      }).catch(() => {
        this.listLoading = false
      })
    },
    confirm() {
      if (this.dialogType == 1) {
        // const addSupplierData = { 'babySuppliesName': this.milkName, 'babySuppliesType': this.babySuppliesType }
        createAdmin(this.accountModel).then(response => {
          this.getList()
        })
      } else if (this.dialogType == 2) {
        // const modifySupplierData = { 'id': this.editSupplierId, 'babySuppliesName': this.milkName }
        updateAdmin(this.accountModel).then(response => {
          this.getList()
        })
      }
    },
    getStr(adata) {
      if (adata === 1) {
        return '是'
      }
      return '否'
    },
    startAdd() {
      this.dialogFormVisible = true
      this.dialogType = 1
      this.dialogTitle = '添加'
      this.accountModel = {}
    },
    deleteAccount(adminId, username) {
      this.$confirm('确定删除: ' + username + ' ？')
        .then(_ => {
          const resetData = { 'id': adminId }
          deleteAdmin(resetData).then(response => {
            this.getList()
          })
        })
        .catch(_ => {})
    },
    isStatusSwitch(rowId, status) {
      if (status == 1) {
        status = 2
      } else {
        status = 1
      }
      this.listLoading = true
      const queryData = { 'id': rowId, 'status': status }
      updateStatusAdmin(queryData).then(response => {
        if (response.data.data == 1) {
          for (var i = 0; i < this.list.length; i++) {
            if (this.list[i].id == rowId) {
              this.list[i].status = status
              break
            }
          }
        }
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    }
  }
}
</script>
